package common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;


public class AutotestUtils {
    //public static EdgeDriver driver;
    public static ChromeDriver driver;


    /**
     * 创建驱动对象
     */
    public static ChromeDriver createDriver() {
        if (driver == null) {
            System.setProperty("webdriver.chrome.driver", "D:/chromedriver-win64/chromedriver.exe");
            //WebDriverManager.chromedriver().setup();
            //WebDriverManager.edgedriver().setup();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            // options.addArguments("-headless");
            //driver = new EdgeDriver(options);
            driver = new ChromeDriver(options);
            //创建隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        }

        return driver;
    }
    /**
     * 获取时间戳
     */
    /*public List<String> getTime() {
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd");
        String filename = sim1.format(System.currentTimeMillis());
        String dirname = sim2.format(System.currentTimeMillis());
        List<String> list = new ArrayList();
        list.add(dirname);
        list.add(filename);
        return list;
    }*/
    /**
     * 获取屏幕截图
     */
    /*public void getScreenShot(String str) throws IOException {
        List<String> list = this.getTime();
        String filename = "./src/test/image/" + list.get(0) + "/" + str + "_" + list.get(1) + ".png";
        File srcfile = (File)driver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcfile, new File(filename));
    }*/
    public  void getScreenShot(String str) throws IOException {
        // ./src/test/image/
        //                  /2024-07-17/
        //                              /test01-17453012.png
        //                              /test02-17453034.png
        //                  /2024-07-18/
        //                              /test01-17453034.png
        //                              /test02-17453045.png
        // 屏幕截图
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        // ./src/test/image/2024-07-17/test01-174530.png
        String filename = "./src/test/image/" + dirTime + "/" + str + "-" + fileTime + ".png";
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // srcFile放到指定位置
        FileUtils.copyFile(srcFile, new File(filename));
    }

}